package com.ulook.lookplanet.controller;

import com.ulook.lookplanet.bean.*;
import com.ulook.lookplanet.common.ExceptionCode;
import com.ulook.lookplanet.common.JsonResult;
import com.ulook.lookplanet.service.Pub2Service;
import com.ulook.lookplanet.service.PubService;
import com.ulook.lookplanet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * 描述：
 *
 * @author L1998
 * @date 2021/4/10
 */
@RestController
@RequestMapping("/pub")
public class PubController {

    @Autowired
    private PubService service;

    /*@Autowired
    private Pub2Service service;*/

    /**
     * 获取话题分组
     */
    @PostMapping("/getComList")
    public JsonResult<List<TCommunity>> getComList(HttpServletRequest request){
        List<TCommunity> communities = service.getComList();
        return new JsonResult<>(communities);
    }

    /**
     * 获取话题
     */
    @PostMapping("/getTopicByComId")
    public JsonResult<List<TTopic>> getTopicByComId(HttpServletRequest request){
        String comId = request.getParameter("comId");
        if(StringUtil.isEmpty(comId)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        List<TTopic> topics = service.getTopicByComId(comId);
        return new JsonResult<>(topics);
    }

    /**
     * 上传图片，保存动态
     */
    @PostMapping("/pubArticle")
    public JsonResult<TArtPic> pubArticle(HttpServletRequest request){

        String userId = request.getParameter("userId");
        String uid = request.getParameter("uid");

        if(StringUtil.isEmpty(uid) || StringUtil.isEmpty(userId)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "未登录");
        }
        TArticle article = new TArticle();
        article.setUid(uid);
        article.setAuthor(Integer.parseInt(userId));
        article.setContent(request.getParameter("content"));
        article.setAudrofile(request.getParameter("audio"));
        article.setHaspic(Integer.parseInt(request.getParameter("hasPic")));
        article.setHastopic(Integer.parseInt(request.getParameter("hasTopic")));
        article.setComuid(request.getParameter("comuId"));

        List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("files");
        for(MultipartFile file : files){
            System.out.println(file.getOriginalFilename()+"   ---------   "+file.getName());
        }
        service.pubArticle(article, files);
        return new JsonResult<>();
    }

    /**
     * 保存选中的话题
     */
    @PostMapping("/addArtTopic")
    public JsonResult addArtTopic(HttpServletRequest request){
        String artId = request.getParameter("artId");
        String topicId = request.getParameter("topicId");
        if(StringUtil.isEmpty(artId) || StringUtil.isEmpty(topicId)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }

        TArtTopic artTopic = new TArtTopic();
        artTopic.setArtid(artId);
        artTopic.setTopicid(topicId);

        service.addArtTopic(artTopic);
        return new JsonResult();
    }
}
